package cn.ilikes.tools.nosql.jpa.metadatas;

import java.util.HashMap;
import java.util.Map;

import cn.ilikes.tools.nosql.jpa.JPASQLPersistenUnitEnv;
import cn.ilikes.tools.nosql.jpa.metadatas.elements.EntityFields;
import cn.ilikes.tools.nosql.jpa.metadatas.elements.EntityIndex;

/**
 * 实体的元数据
 * 
 * @author fack
 * 
 */
public class EntityMetaData {

	private JPASQLPersistenUnitEnv hbasePersistenUnitEnv;

	private Class<?> entityClazz;

	private Class<?>[] interfaces;

	private String entityTableName;

	private String entityName;

	private String puName;

	private EntityFields idEntityFields;

	//entity can index 
	private EntityIndex entityIndex;

	private String schema;

	/**
	 * KEY : fieldName
	 */
	private Map<String, EntityFields> entityFieldsMap = new HashMap<String, EntityFields>();

	public EntityFields getEntityId() {
		return idEntityFields;
	}

	public void setEntityFieldsId(EntityFields entityFieldsId) {
		this.idEntityFields = entityFieldsId;
	}

	public EntityMetaData(String puName, Class<?> entityClazz, JPASQLPersistenUnitEnv hbasePersistenUnitEnv) {
		this.entityClazz = entityClazz;
		this.puName = puName;
		this.hbasePersistenUnitEnv = hbasePersistenUnitEnv;
	}

	public String getEntityTableName() {
		return entityTableName;
	}

	public void setEntityTableName(String entityName) {
		this.entityTableName = entityName;
	}

	public Class<?> getEntityClazz() {

		return entityClazz;
	}

	public EntityIndex getEntityIndex() {

		return entityIndex;
	}

	public Map<String, EntityFields> getEntityFields() {

		return entityFieldsMap;
	}

	public EntityFields getEntityField(String fieldName) {
		EntityFields entity = entityFieldsMap.get(fieldName);
		//				if (entity == null) {
		//					entity = new EntityFieldsImpl();
		//					entityFieldsMap.put(fieldName, entity);
		//				}
		return entity;
	}

	public boolean isUseSecondryIndex() {

		return true;
	}

	public void setEntityClazz(Class<?> entityClazz) {
		this.entityClazz = entityClazz;
	}

	public void setEntityIndex(EntityIndex entityIndex) {
		this.entityIndex = entityIndex;
	}

	public boolean isEntityableIndex() {
		if (this.entityIndex != null && this.entityIndex.isIndexable()) {
			return true;
		}
		return false;
	}

	public String getPuName() {

		return puName;
	}

	public void setPuName(String puName) {
		this.puName = puName;
	}

	public String getEntityName() {

		return entityName;
	}

	public Class<?>[] getInterfaces() {

		return interfaces;
	}

	public void setInterfaces(Class<?>[] interfaces) {
		this.interfaces = interfaces;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}

	public EntityFields findEntityFieldsByJPAColumn(String jpaColumnName) {
		for (EntityFields entityField : entityFieldsMap.values()) {
			try {
				if (entityField.getjPAField().getColumnName().equals(jpaColumnName))
					return entityField;
			} catch (NullPointerException e) {
				continue;
			}
		}
		return null;
	}

	public JPASQLPersistenUnitEnv getJPAHbasePersistenUnitEnv() {
		return hbasePersistenUnitEnv;
	}

	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

}
